package com.example.springbootwebdemo.controller;

import com.example.springbootwebdemo.entity.Product;
import com.example.springbootwebdemo.repository.ProductRepository;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.*;

import javax.annotation.PostConstruct;
import javax.annotation.Resource;

@Controller
public class ProductController {
    @Resource
    private ProductRepository productRepository;

    @DeleteMapping("/product/{id}")
    public String delete(@PathVariable Integer id) {
        productRepository.deleteById(id);
        return "success";
    }

    @GetMapping("/product/{id}")
    @ResponseBody
    public Product findById(@PathVariable Integer id) {
        return productRepository.findById(id);
    }

    @PostMapping("/product/update")
    public String update(Product product) {
        productRepository.update(product);
        return "redirect:/user/list";
    }
}
